package cn.restcloud.example.dao.impl;

import java.util.List;

import cn.restcloud.example.dao.IDemoUserDao;
import cn.restcloud.example.model.UserModel;
import cn.restcloud.framework.core.annotation.bean.BeanConfig;
import cn.restcloud.framework.core.annotation.bean.BeanType;
import cn.restcloud.framework.core.base.IBasePageBean;
import cn.restcloud.framework.core.base.RdbBaseDao;
import cn.restcloud.framework.core.base.RdbPageParams;
import cn.restcloud.framework.core.container.BeanFactory;

import org.apache.commons.lang.StringUtils;
import org.bson.Document;

import cn.restcloud.framework.core.util.BeanUtil;
import cn.restcloud.framework.core.util.DocumentUtil;
import cn.restcloud.framework.core.util.PrintUtil;
import cn.restcloud.framework.core.util.RequestUtil;
import cn.restcloud.framework.core.util.db.rdb.RdbPageUtil;
import cn.restcloud.framework.core.util.db.rdb.RdbUtil;


/**
 * DemoDao Rdb关系数据库实现类
 * @author Administrator
 *
 */

@BeanConfig(appId = "example",beanId = "DemoUserRdb",beanName = "Demo示例Dao Rdb实现类",beanType = BeanType.DAO,autowired = false,singleton = true,hotMode = true)
public class DemoUserRdbImpl extends RdbBaseDao<UserModel>  implements IDemoUserDao {
	
	private String keyId;

	public DemoUserRdbImpl() throws Exception{
		keyId=BeanUtil.getBeanKeyId(UserModel.class); //从bean中获取注解的关键字段名
	}
	
	/**
	 * 根据_id获取对像
	 * @param beanId
	 * @return
	 * @throws Exception
	 */
	@Override
	public UserModel getById(String id) throws Exception {
		return RdbUtil.getBeanByKeyId(super.getTableName(), id, keyId, UserModel.class);
	}
	

	/**
	 * 保存对像
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	@Override
	public int save(UserModel obj) throws Exception {
		int i = RdbUtil.saveBean(getTableName(), obj);
		return i;
	}

	/**
	 * 根据Document的_id删除一条文档
	 * @param _id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteById(String _id) throws Exception {
		return RdbUtil.deleteByKeyId(getTableName(), _id, keyId);
	}
	
	/**
	 * 根据Document的_id删除多条文档,ids多个用逗号分隔
	 * @param _id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByIds(String _ids) throws Exception {
		int i=0;
		String[] idArray=_ids.split(",");
		for(String id:idArray){
			this.deleteById(id);
			i++;
		}
		return i;
	}
	
	/**
	 * 根据bean对像进行删除
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	@Override
	public int delete(UserModel obj) throws Exception {
		return RdbUtil.deleteByBean(getTableName(), obj);
	}


	@Override
	public IBasePageBean<UserModel> listByPage() throws Exception {
		return RdbPageUtil.listByPage(this,"*","order by editTime desc");
	}


	@Override
	public UserModel getByUserId(String userId) throws Exception {
		String sql="select * from "+this.getTableName()+" where userId=?"; //通过参数传入变量可以防止sql注入攻击
		return RdbUtil.getBean(UserModel.class, sql, userId);
	}

	@Override
	public List<UserModel> listAll() throws Exception {
		return RdbUtil.listAllBeans(UserModel.class, this.getTableName());
	}

	@Override
	public IBasePageBean<UserModel> listByPageAndState(String state) throws Exception {
		// 这里进行指定条件的数据查询并分页
		String sqlWhere="where state='"+RdbUtil.formatSqlValue(state)+"'";
		String orderBy="order by editTime desc";
		RdbPageParams pageParams=RdbPageUtil.getPageParams(RdbUtil.getConnection(), this.getTableName(), "*", sqlWhere, orderBy);
		IBasePageBean<UserModel> pageBean=super.listByPage(pageParams, UserModel.class);
		return pageBean;
	}

	@Override
	public IBasePageBean<UserModel> listByPageAndStateAndFilters(String state)
			throws Exception {
		// 这里进行指定条件的数据查询并分页
		String sqlWhere="where state='"+RdbUtil.formatSqlValue(state)+"'";
		String orderBy="order by editTime desc";
		//这里多增加了一个true表示可以合并url中传入进来的动态过滤条件为sql where组合
		RdbPageParams pageParams=RdbPageUtil.getPageParams(RdbUtil.getConnection(), this.getTableName(), "*", sqlWhere, orderBy,true); 
		IBasePageBean<UserModel> pageBean=super.listByPage(pageParams, UserModel.class);
		return pageBean;
	}

	@Override
	public IBasePageBean<UserModel> listByPageBySqlWhere(String state)
			throws Exception {
		//先创建一个pagebean对像
		IBasePageBean<UserModel> pageBean=(IBasePageBean<UserModel>)BeanFactory.getBean("PageBean");

		String urlPageNo=RequestUtil.getString("pageNo");//从url请求中获取当前页参数
		String urlPageSize=RequestUtil.getString("pageSize");//从url请求中获取分页显示数
		if(StringUtils.isBlank(urlPageSize)){urlPageSize="20";}
		if(StringUtils.isBlank(urlPageNo)){urlPageNo="1";}
		
		//初始化分页数据
		int pageSize=Integer.parseInt(urlPageSize);
		if(pageSize==0){pageSize=DEFAULT_PAGESIZE;}
		pageBean.setPageNo(Integer.parseInt(urlPageNo)); //当前第几页
		pageBean.setPageSize(pageSize); //每页显示数
		pageBean.setSqlTableName(this.getTableName()); //要查询的数据库表
		pageBean.setSqlFields("*");//要选择的表字段，这里*号全选
		pageBean.setSqlOrderby("order by editTime"); //必须指定一个排序字段
		pageBean.setSqlWhere("where state='"+state+"'"); //完全自定义组合一个sql where条件
		
		RdbUtil.listBeansByPage(RdbUtil.getConnection("mysql"),pageBean, UserModel.class);
		
		return pageBean; //返回分页结果
	}
}
